<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css"></style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="inquiryQuotationForm" th:action="@{/inquiryQuotation/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>汇总号：</label>
                            <input id="inquiryTotalCode" name="inquiryTotalCode" type="text"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料编码：</label>
                            <input id="materialCode" name="materialCode" type="text"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料描述：</label>
                            <input id="materialDesc" name="materialDesc" type="text"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>采购组：</label>
                            <select name="purchGroup" sysDict="CGZ" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3">
                            <label>状态：</label>
                            <select id="state" name="state">
                                <option></option>
                                <option value="-1" selected>草稿</option>
                                <option value="0">已报价</option>
                                <option value="1">报价单已提交</option>
                                <option value="2">报价单审核中</option>
                                <option value="3">审核通过</option>
                                <option value="-3">审核驳回</option>
                            </select>
                        </li>
                        <li class="col-sm-3">
                            <label>询价报价单号：</label>
                            <input id="inquiryCode" name="inquiryCode" type="text"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%" id="factory">
                            <label>工厂：</label>
                            <select name="werks" sysDict="GC" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3">
                            <button id="inquiryQuotationSearchBtn" type="button"
                                    class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="inquiryQuotationSearch" type="button" class="btn btn-primary btn-rounded btn-sm"
                                    style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="inquiryQuotationToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="inquiryQuotationadd()">
                    <i class="fa fa-plus"></i> 报价
                </a>
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="inquiryQuotationedit()">
                    <i class="fa fa-edit"></i> 编辑
                </a> -->
                <a class="btn btn-outline btn-success btn-rounded" onclick="downloadFiles()">
                    <i class="fa fa-download"></i> 下载附件
                </a>
                <!--<a class="btn btn-outline btn-success btn-rounded" data-toggle='modal' onclick="show()">
                   <i class="fa fa-envelope"></i> 发送邮件
               </a>-->
            </div>
            <div>
                <table id="inquiryQuotation" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div id="myMailModal" style="display: none">
    <div class="modal-body">
        <div class="form-group">
            <label>收件人</label><label style="color: red">(多个收件人以英文字符 ; 分开)</label>
            <textarea id="email" class="form-control" rows="5"></textarea>
        </div>
        <br/>
        <div class="form-group">
            <label>内容</label>
            <textarea id="remark" class="form-control" rows="5"></textarea>
        </div>
        <div class="form-group">
            <label>供应商</label>
            <textarea id="vendorName" class="form-control" rows="5"></textarea>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal" onclick="closeDialog()">关闭</button>
        <button type="button" class="btn btn-primary" data-dissmiss="modal" onclick="sendmail()">发送</button>
    </div>
</div><!-- /.modal-dialog -->

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document" style="width:70%">
        <div class="modal-content" style="height:700px">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">详情</h4>
            </div>
            <div class="modal-body" style="height:90%;width:85%;overflow:auto">
                <table class="table table-bordered text-nowrap" id="mail">
                    <thead>
                    <tr>
                        <th>报价单号</th>
                        <th>邮箱</th>
                        <th>时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="parityEmail : ${parityEmaillist}">
                        <td style="display:none"><input name="parityEmailId" type="hidden"
                                                        th:value="${parityEmail.parityEmailId}"/></td>
                        <td width='110px'><input style='width:95%' type='text' name='parityEmailInquiryCode'
                                                 th:value="${parityEmail.parityEmailInquiryCode}"/></td>
                        <td width='110px'><input style='width:95%' type='text' name='parityEmail'
                                                 th:value="${parityEmail.parityEmail}"/></td>
                        <td width='110px'><input style='width:95%' type='text' name='data'
                                                 th:value="${parityEmail.data}"/></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        </div>
    </div>&lt;!&ndash; /.modal-content &ndash;&gt;
</div><!-- /.modal-dialog -->
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    $(function () {
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        $("#email").keydown(function (event) {
            if (event.keyCode == "13") {
                e = $(this).val().replace(/[\r\n]/g, "");
                arr = e.split(";");
                for (var i = 0; i < arr.length; i++) {
                    alert(arr[i]);
                    if (!myreg.test(arr[i])) {
                        alert('提示\n\n请输入有效的E_mail！');
                        //myreg.focus();
                        return false;
                    }
                }
                $(this).val(e + ';');
            }
        });

        $("#remarks").keydown(function (event) {
            if (event.keyCode == "13") {
                e = $(this).val();
                $(this).val(e + ',');
            }
        });

        $("#vendorNameid").keydown(function (event) {
            if (event.keyCode == "13") {
                e = $(this).val();
                $(this).val(e + ';');
            }
        });
    });

</script>
<script type="text/javascript">
    //面向对象
    var searcherBS = Searcher.createBS('#inquiryQuotation',
        [
            {radio: true},
            {title: "询价报价单号", field: "inquiryCode"},
            //状态：-1 报价单草稿, 0 已报价,1 报价单已提交,2 报价单审核中,3.审核通过,-3审核驳回
            {
                title: "状态 ", field: "state",
                formatter: function (value, row, index) {
                    if (value == -1) {
                        return "草稿";
                    } else if (value == 0) {
                        return "已报价";
                    } else if (value == 1) {
                        return "报价单已提交";
                    } else if (value == 2) {
                        return "报价单审核中";
                    } else if (value == 3 || value == 4 || value == 5) {
                        return "审核通过";
                    } else if (value == -3) {
                        return "审核驳回";
                    }
                }
            },
            {title: "汇总号", field: "inquiryTotalCode"},
            {title: "采购申请编码", field: "requestCode"},
            {title: "物料描述", field: "materialDesc"},
            {title: "物料编码", field: "materialCode"},
            {title: "数量", field: "purchaseCnt"},
            {title: "含税价", field: "price"},
            {title: "最小起订量", field: "priceUnit"},
            {title: "订单单位", field: "poPriceUnit"},
            {title: "税代码", field: "taxCode"},
            {title: "总价", field: "totalPrice"},
            {title: "工厂", field: "werks"},
            {title: "回复交期", field: "replyDeliveryDate"},
            {title: "供应商编码", field: "vendorCode"},
            {title: "供应商名称", field: "vendorName"},
            /*{title: "邮箱",field:"email"},*/
            /*{title: "详情",field:'',
                formatter: function(value,row,index){
                    //return '<a style="color:blue" href="#" onclick=fun("' + row.inquiryCode + '");>邮箱详情</a>';
                    return '<a class="btn btn-outline btn-success btn-rounded" data-toggle="modal" data-target="#myModal" onclick=fun("' + row.inquiryCode + '");>邮箱详情</a>';
                }
            },*/
            {
                title: "附件", field: 'attachment',
                formatter: function (value, row, index) {
                    if (value) {
                        return "<img src='" + ctxPath + "/img/duicuo/dui.png' width='20px' />";
                    }
                    return "<img src='" + ctxPath + "/img/duicuo/cuo.png' width='20px' />";
                }
            }
        ], {pageList: [5, 10, 15, 50, 100], pageSize: 10}
    );

    //刷新数据
    function refresh() {
        searcherBS.refresh();
    }


    $(function () {
        initDictDataSel();//初始化数据字典
        infoto();//权限设置
    })

    //设置确认的权限
    function infoto() {
        Page.ajaxPOST(ctxPath + "/sys/role/findRoleName", null, function (data) {
            var roleName = data.result;
            for (var i in roleName) {
                console.log(roleName[i]);
                if (roleName[i] == "采购专员") {
                    $("#factory").css({'display': "none"});
                }
            }

        });
    }

    /**
     * 填写报价单
     */
    function inquiryQuotationadd() {
        var rowDatas = searcherBS.getSelectedRows("quotationId");
        var states = searcherBS.getSelectedRows("state");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        for (var i = 0; i < states.length; i++) {
            if (states[i] != -1) {
                Dialog.errorMsg("此状态不允许报价");
                return;
            }
        }
        var index = Dialog.openUrl("新增报价单", ctxPath + '/inquiryQuotation/toEdit/' + rowDatas,
            {
                area: ['900px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index)
    }

    function fun(obj) {
        var inquiryCode = obj;
        $.ajax({
            type: "GET",
            url: ctxPath + '/parityEmail/findById/' + inquiryCode,
            contentType: "application/json;charsetset=UTF-8",//必须
            dataType: "json",//将JSONObj作为请求参数传递 ;
            cache: true,
            async: false,
            error: function (request) {
                showTipAlert("Connection error");
            },
            success: function (res) {
                var data = eval(res.result);
                var trCode = "";
                for (var item in data) {
                    trCode += "<tr>";
                    trCode += "<td style='width:5%'><input name='parityEmailInquiryCode' value='" + data[item].parityEmailInquiryCode + "' /></td>";
                    trCode += "<td style='width:200%'><input name='parityEmail' value='" + data[item].parityEmail + "' style='width:100%' /></td>";
                    trCode += "<td style='width:95%'><input name='data' value='" + data[item].data + "' /></td>";
                    trCode += "</tr>";
                }
                $("#mail tbody").html("");
                $("#mail tbody").append(trCode);

            }
        });
    }

    //显示发送邮件弹出框
    function show() {
        var rowData = searcherBS.getRawData();
        if (rowData.length == 0) {
            Dialog.errorMsg("至少选择一项发送邮件！");
            return;
        }
        var email = '';
        var vendorNames = '';
        for (var index in rowData) {
            if (rowData[index].email != null && rowData[index].email != '') {
                email += rowData[index].email + ';';
            }
            vendorNames += rowData[index].vendorName + ';';
        }
        email = email.substr(0, email.length - 1);
        vendorNames = vendorNames.substr(0, vendorNames.length - 1);
        $('#remark').val('');
        $('#vendorName').val(vendorNames);
        $('#email').val(email);
        Dialog.open("发送邮件", $("#myMailModal"), {area: ['800px', '650px']});
    }

    //关闭模态框
    function closeDialog() {
        layer.closeAll();
    }

    function close() {
        $("#myMailModal").modal('hide');
    }

    //发送邮件
    function sendmail() {
        //验证信息
        var reg = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        var email = $("#email").val();
        if (email == null || email.trim() == "") {
            Dialog.errorMsg("邮箱不能为空！");
            return;
        }
        email = $("#email").val().replace(/[\s]/g, "").split(';');
        for (index in email) {
            if (email[index] == "" || email[index] == null || typeof (email[index]) == undefined) {

                email.splice(index, 1);
            } else if (!reg.test(email[index].trim())) {
                Dialog.errorMsg("邮箱格式不正确！");
                return;
            }
        }
        var remark = $("#remark").val().trim();
        if (remark == null || remark == "" || remark == undefined) {
            Dialog.errorMsg("内容不能为空！");
            return;
        }
        var vendorName = $("#vendorName").val().trim();
        if (vendorName == null || vendorName == "" || vendorName == undefined) {
            Dialog.errorMsg("供应商信息不能为空！");
            return;
        }
        var arremail = JSON.stringify(email);

        var myMap = {};
        myMap["arremail"] = arremail;
        myMap["content"] = remark + "\n请登录网站查看：http://" + location.host + "/hssrm/sys/index";
        myMap["vendorName"] = vendorName;
        $.ajax({
            type: "POST",
            url: ctxPath + "/parityEmail/offerSendMail",
            data: JSON.stringify(myMap),//必须
            contentType: "application/json;charset=UTF-8",//必须
            cache: true,
            error: function (data) {
                Dialog.msg("发送失败，请重试！");
                closeDialog();
            },
            success: function (data) {
                if (data.result == "ok") {
                    Dialog.msg("发送成功！");
                    closeDialog();
                } else {
                    Dialog.msg("发送失败，请重试！");
                    closeDialog();
                }
            }
        });
    }

    //下载附件
    function downloadFiles() {
        var rowData = searcherBS.getRawData();
        if (rowData.length != 1) {
            Dialog.errorMsg("选择一项下载附件！");
            return;
        }
        if (rowData[0].attachment == null || rowData[0].attachment == undefined || rowData[0].attachment == '') {
            Dialog.errorMsg("没有可下载的文件！");
            return;
        }
        var inquiryItemId = rowData[0].inquiryItemId;
        var url = ctxPath + "/inquiryQuotation/downloadFile";
        jQuery('<form action="' + url + '" method="post">' +
            '<input type="text" name="inquiryItemId" value="' + inquiryItemId + '"/></form>')
            .appendTo('body').submit().remove();
    }

</script>
</body>
</html>